package com.sl.au.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.MyMonetTotalView;
import com.sl.au.entity.Option;
import com.sl.au.entity.SaleArea;
import com.sl.au.entity.User;
import com.sl.au.service.ContractService;
import com.sl.au.service.MyMoneyTotalViewService;
import com.sl.au.service.OptionService;
import com.sl.au.service.SaleAreaService;

@RestController
@RequestMapping(value = "/ui/myMoneyTotal")
public class myMoneyTotalController extends BaseController {
	
	@Autowired
	OptionService optionService;
	
	@Autowired
	SaleAreaService saleAreaService;
	
	@Autowired
	MyMoneyTotalViewService myMoneyTotalViewService;
	
	@Autowired
	ContractService contractService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("myMoneyTotal/myMoneyIndex");
		System.out.println(request.getParameter("deptNameValue"));
		String[] areaNames = null;
		String[] superiors = null;
		if(StringUtils.hasText(request.getParameter("arearNameValue")) && request.getParameter("arearNameValue")!= null){
			areaNames = request.getParameter("arearNameValue").toString().split(",");//片区
		}
		if(StringUtils.hasText(request.getParameter("deptNameValue")) && request.getParameter("deptNameValue")!= null){
			superiors = request.getParameter("deptNameValue").toString().split(",");//部门
		}
		List<SaleArea> orderAreas = saleAreaService.getRepository().getAllSaleArea();
		List<SaleArea> SaleOrderAreas = new ArrayList<SaleArea>();//销售
		List<SaleArea> CompleteOrderAreas = new ArrayList<SaleArea>();//成套
		List<SaleArea> ForeignOrderAreas = new ArrayList<SaleArea>();//外贸
		for(SaleArea orderArea : orderAreas){
			orderArea.setIsSelected(false);
			if(orderArea.getSuperior().equals("销售处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
				 		 	if(areaNames[i].equals(orderArea.getAreaName()))
						 		orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("销售处-分管领导")){
				}else{
					if(orderArea.getAreaName().equals("科长")){
					}else{
						SaleOrderAreas.add(orderArea);
					}
				}
			}
			if(orderArea.getSuperior().equals("成套处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getAreaName()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("其他")){
				}else{
					if(orderArea.getAreaName().equals("成套处分管领导")){
					}else{
						CompleteOrderAreas.add(orderArea);
					}
				}
			}
			if(orderArea.getSuperior().equals("外贸处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getAreaName()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("外贸处-分管领导")){
				}else{
					ForeignOrderAreas.add(orderArea);
				}
			}
		}
		Account aaa = this.getMyAccount();
		modelAndView.addObject("SaleOrderAreas", SaleOrderAreas);
		modelAndView.addObject("CompleteOrderAreas", CompleteOrderAreas);
		modelAndView.addObject("ForeignOrderAreas", ForeignOrderAreas);
		String superior = aaa.getUser().getSaleArea().getSuperior();
		
		modelAndView.addObject("superior", superior);
		List<Option> lists = optionService.getRepository().findByGroup("费用管理");
		String typeName = request.getParameter("typeNameValue")!=null ? request.getParameter("typeNameValue") : "";
		List<Option> l = new ArrayList<Option>();
		for(Option list : lists){
			list.setIsSelected(false);
			if(typeName.equals("其他费")){
				if(list.getTitle().indexOf("其他") != -1){
					list.setIsSelected(true);
				}
			}else if(typeName.equals("车船费")){
				if(list.getTitle().indexOf("小车") != -1){
					list.setIsSelected(true);
				}
			}else if(typeName.equals("办事处")){
				if(list.getTitle().indexOf("办事") != -1){
					list.setIsSelected(true);
				}
			}else{
				if(list.getTitle().equals(typeName)){
					list.setIsSelected(true);
				}
			}
			l.add(list);
		}
		
		modelAndView.addObject("l",l);//费用管理
		modelAndView.addObject("industryInfo1", request.getParameter("typeNameValue")!=null ? request.getParameter("typeNameValue") : "");//
		
		String personName = request.getParameter("personNameValue") != null ? request.getParameter("personNameValue") : "";
		List<User> allSuperiorMen = contractService.getAllSuperiorMenBySuperior(superior);
		List<User> p = new ArrayList<User>();
		for(User person : allSuperiorMen){
			person.setIsSelected(false);
			if(person.getEmpLogin().getName().equals(personName)){
				person.setIsSelected(true);
			}
			p.add(person);
		}
		modelAndView.addObject("p",p);//申请人
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		
		String s = df.format(new Date());
		Calendar a=Calendar.getInstance(); 
		int aa = a.get(Calendar.YEAR);
		modelAndView.addObject("startDate", request.getParameter("beginDate")!= null ? request.getParameter("beginDate") : a.get(Calendar.YEAR)+"-01-01");//开始时间
		modelAndView.addObject("endDate", request.getParameter("endDate")!=null ? request.getParameter("endDate") : df.format(new Date()));//结束时间
		modelAndView.addObject("nameDate", request.getParameter("dateTimeType")!=null?request.getParameter("dateTimeType"):"");//时间类型
		modelAndView.addObject("personName", request.getParameter("personNameValue") != null ? request.getParameter("personNameValue") : "" );//申请人
		return modelAndView;
	}
	
	
	//获取主界面列表中的数据
			@SuppressWarnings("unused")
			@RequestMapping(value = "getMyMoneyTotalPage", method = RequestMethod.GET)
			public Map<String, Object> getMyMoneyTotalPage(HttpServletRequest request) {
				Map<String, Object> map = new HashMap<String, Object>();
				Map<String, Object> qMap = new HashMap<String, Object>();
				Map<String, Object> condition = new HashMap<String, Object>();
				Map<String, Object>numMap=new HashMap<String, Object>();
				int start = Integer.parseInt(request.getParameter("start"));
				int length = Integer.parseInt(request.getParameter("length"));
				Account a = this.getMyAccount();
				String dataFilter = "";
				User user = a.getUser();
				this.getQueryParams(qMap,numMap);
				condition.put("qMap", qMap);
				condition.put("numMap", numMap);
				
				String userId = a.getUser().getId();
				String Superior = a.getUser().getSaleArea().getSuperior();//权限部门
				String areaName = a.getUser().getSaleArea().getAreaName();
				if (userId == null) {
					userId = "";
				}
				if (Superior == null) {
					Superior = "";
				}
				if (areaName == null) {
					areaName = "";
				}
				if (dataFilter == null) {
					dataFilter = "";
				}
				condition.put("userId", userId);
				condition.put("superior", Superior);
				condition.put("areaName", areaName);
				condition.put("dataFilter", dataFilter);
				List<MyMonetTotalView> contracts = myMoneyTotalViewService.getMyMoneyTotal(start, length, condition, user, "applyDate", "desc");
				List<MyMonetTotalView> contractsum = myMoneyTotalViewService.getMyMoneyTotal(0, Integer.MAX_VALUE, condition, user, "applyDate","desc");
				long count = contractsum.size();
				map.put("data", contracts);
				map.put("draw", request.getParameter("draw"));
				map.put("recordsFiltered", count);
				map.put("recordsTotal", count);
				return map;
			}
			
			/**
			 * hql
			 * @param qMap
			 * @param numMap
			 */
				private void getQueryParams(Map<String, Object> qMap,Map<String, Object>numMap){
					String names[]={"sSuperiorArea","applyUserName","kind"};//部门-片区，申请人，费用类型
					String dates[] = {"applyDate"};//申请日期
					for(String name:names){
						System.out.println(request.getParameter(name));
						String aa =  request.getParameter(name);
						if(StringUtils.hasText(request.getParameter(name))){
							String[] a = request.getParameter(name).toString().split(",");
							String b = "";
							for(int i=0;i<a.length;i++){
								b += "'" + a[i] + "',";
							}
							b = b.substring(0, b.length()-1);
							numMap.put(name, b);
						}
					}
					if(StringUtils.hasText(request.getParameter("memo"))){//事由、备注
						numMap.put("memo", request.getParameter("memo").toString());
					}
					for(String date:dates){
						if(null!=this.getDateQueryItem(date)){
							qMap.put(date,this.getDateQueryItem(date));
						}
					}
					
				}
}
